﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Bussiness.Manager;
using Bussiness.Entity;
using System.Configuration;
using System.Threading;
namespace DSC
{
    public partial class AfUserControl : UserControl
    {
        public Main ParentForm;
        private void CheckPermission(RoleFunction role, Control ctl)
        {

            foreach (Control ctlChild in ctl.Controls)
            {
                if (ctlChild.GetType().BaseType != typeof(AfUserControl))
                {
                    if (ctlChild.Tag != null && !string.IsNullOrEmpty(ctlChild.Tag.ToString()) && ctlChild.Tag.ToString().ToLower() != "false")
                    {
                        if (!role.HasFunction(ctlChild.Tag.ToString()))
                            ctlChild.Enabled = false;
                    }
                    CheckPermission(role, ctlChild);
                }
            }
        }
        private void CheckPermission()
        {

            RoleFunction role = SessionLogin.GetRoleFunctionOfForm(this.Name);
            if (role != null)
            {
                foreach (Control ctl in this.Controls)
                {
                    if (ctl.GetType().BaseType != typeof(AfUserControl))
                    {
                        if (ctl.Tag != null && !string.IsNullOrEmpty(ctl.Tag.ToString()) && ctl.Tag.ToString().ToLower() != "false")
                        {
                            if (!role.HasFunction(ctl.Tag.ToString()) && ctl.Tag.ToString().ToLower() != "false")
                                ctl.Enabled = false;
                        }
                        CheckPermission(role, ctl);
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn không có quyền vào chức năng này.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Enabled = false;
            }

        }
        public void ShowStatusBar(string message)
        {
            if (ParentForm != null)
            {
                ParentForm.ShowStatusMessage(message);            
            }
        }
        public void EndShowStatusBar()
        {
            if (ParentForm != null)
            {
                ParentForm.EndShowStatusMessage();
            }
        }
        public AfUserControl()
        {
            InitializeComponent();
        }

        protected void Permission()
        {
            if (ConfigurationSettings.AppSettings["Mode"].ToLower() == "run")
            {
                CheckPermission();
            }
            else if (ConfigurationSettings.AppSettings["Mode"].ToLower() == "collect")
            {
                CollectFunction();
            }

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //dong code neu dang o che do design
            Permission();
        }

        public void ShowChildForm(AfChildrenForm frm)
        {
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.ChangeData += new EventHandler(ChildrenChangeData);
            frm.ShowDialog();

        }

        public void ChildrenChangeData(object obj, EventArgs e)
        {
            Search();
        }

        public virtual void Search()
        {

        }
        /// <summary>
        /// Luu thong tin cac function co trong form
        /// </summary>
        /// <param name="ctl"></param>
        private void CollectFunction(Control ctl)
        {
            foreach (Control ctlChild in ctl.Controls)
            {
                if (ctlChild.GetType() != typeof(AfUserControl))
                {
                    if (ctlChild.Tag != null && !string.IsNullOrEmpty(ctlChild.Tag.ToString()) && ctlChild.Tag.ToString() != "False")
                    {
                        SaveFunction(this.Name, ctlChild.Tag.ToString());
                    }
                    CollectFunction(ctlChild);
                }
            }
        }
        /// <summary>
        /// Luu thong tin cac function co trong form
        /// </summary>
        private void CollectFunction()
        {
            if (PT_FormManager.CreateInstant().SelectByFormNameLST(this.Name).Count == 0)
            {
                PT_FormEntity obj = new PT_FormEntity();
                obj.FormName = this.Name;
                obj.FormTitle = this.Name;
            }
            foreach (Control ctl in this.Controls)
            {
                if (ctl.Tag != null && !string.IsNullOrEmpty(ctl.Tag.ToString()) && ctl.Tag.ToString() != "False")
                {
                    SaveFunction(this.Name, ctl.Tag.ToString());
                }
                CollectFunction(ctl);
            }

        }
        /// <summary>
        /// Luu thong tin cac function co trong form
        /// </summary>
        private void SaveFunction(string formName, string tag)
        {
            PT_FunctionEntity objCond = new PT_FunctionEntity();
            objCond.FormName = formName;
            objCond.Tag = tag;
            if (PT_FunctionManager.CreateInstant().SelectWithConditionLST(objCond).Count == 0)
            {
                PT_FunctionEntity obj = new PT_FunctionEntity();
                obj.FormName = formName;
                obj.Tag = tag;
                PT_FunctionManager.CreateInstant().Insert(obj);
            }
        }

    }
}
